package com.lws.framework.module.usercenter.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lws.framework.dao.BaseDao;
import com.lws.framework.module.usercenter.bean.User;
import com.lws.framework.module.usercenter.dao.UserDao;
import com.lws.framework.service.BaseService;

@Service
@Transactional
public class UserService extends BaseService<User> implements UserDetailsService {

    @Autowired
    private UserDao userDao;

    @Override
    protected BaseDao<User> getDao() {
        return userDao;
    }

    public Authentication getAuthentication() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return authentication;
    }

    public User getCurrentUser() {
        User user = (User) getAuthentication().getPrincipal();
        return user;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userDao.findByUsername(username);
        return user;
    }

}
